package com.zjw.controller;


import com.zjw.annotation.domain.ResponseResult;
import com.zjw.enums.AppHttpCodeEnum;
import com.zjw.exception.SystemException;
import com.zjw.service.ArticleService;
import io.jsonwebtoken.lang.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/content/article")
@PreAuthorize("@Ps.hasPermission('content:article:list')")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @GetMapping("/list")
    public ResponseResult getArticleList(Long pageNum, Long pageSize,
                                         String title, String summary){
        return articleService.getAllArticleList(pageNum, pageSize, title, summary);
    }


    @GetMapping("/{id}")
    public ResponseResult getEditArticleById(@PathVariable Long id){
        return articleService.getEditArticleById(id);
    }


    @DeleteMapping("/{ids}")
    public ResponseResult deleteArticle(@PathVariable String ids){
        if(!Strings.hasText(ids))
            throw new SystemException(AppHttpCodeEnum.DATA_UNCOMPLETED);

        List<Long> idList = Arrays.stream(ids.split(",")).map(n -> Long.parseLong(n))
                .collect(Collectors.toList());
        return articleService.deleteArticleList(idList);
    }
}
